package com.edgelight.service.impl;

import com.edgelight.dao.NewsMapper2;
import com.edgelight.pojo.News;
import com.edgelight.pojo.req.NewReq;
import com.edgelight.pojo.resp.BaseResp;
import com.edgelight.service.NewsService2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**新闻接口实现类
 * @author Screncea
 */
@Service
public class NewsServiceImpl2 implements NewsService2 {
    @Autowired
    NewsMapper2 newsMapper2;

    /**新闻增加方法
     * @param news News实体类
     * @return baseResp结果集
     */
    @Override
    public BaseResp editorNew(NewReq news) {
        News news1 = new News();
        news1.setId(news.getIdremarkId());
        news1.setNewsTitle(news.getNewsTitle());
        news1.setNewsNeirong(news.getNewsNeirong());
        news1.setNewsCover(news.getNewsCover());
        //将数组转换为字符串,并加上分隔符";",并封装
        String newPics=String.join(";",news.getNewsPics());
        news1.setNewsPics(newPics);
        news1.setNewsDate(news.getNewsDate());
        news1.setNewsDesc(news.getNewsDesc());
        news1.setNewsAuthor(news.getNewsAuthor());
        news1.setNewsIsCheck(news.getNewsIsCheck());
        news1.setNewsClickNum(news.getNewsClickNum());
        news1.setNewsThumbUp(news.getNewsThumbUp());
        news1.setNewsCollection(news.getNewsCollection());
        news1.setIdremarkId(news.getIdremarkId());
        news1.setNewsType(news.getNewsType());
        news1.setNewsExpertNum(news.getNewsExpertNum());
        System.out.println(news1.toString());
        newsMapper2.editorNew(news1);
        return new BaseResp().SUCCESS();
    }
    /**通过新闻类型id查询新闻
     * @param id 新闻类型id
     * @return 结果集
     */
    @Override
    public BaseResp findNewsByTypeId(Integer id) {
        List<News> newsListById = newsMapper2.findAllByTypeId(id);
        if (newsListById!=null){
            return new BaseResp(1,"success",newsListById,null);
        }
        return new BaseResp().ERROR();
    }

    /**根据新闻的点击量查询新闻封面
     * @return list集合
     */
    @Override
    public BaseResp findImage() {
        List<News> imageLimit = newsMapper2.findImage();
        List<String> images = new ArrayList();
        for (int i =0; i < imageLimit.size(); i++){
            System.out.println(imageLimit.get(i).getNewsCover());
            images.add(imageLimit.get(i).getNewsCover());
        }
        if (imageLimit!=null){
            return new BaseResp(1,"success",images,null);
        }else {
            return new BaseResp(0,"暂无更多数据",null,null);
        }

    }
    /**根据新闻id查询新闻内容并且访问量+1
     * @param id 新闻id
     * @return 结果集(新闻内容)
     */
    @Override
    public BaseResp findOneNewById(Integer id) {
        News oneNewById = newsMapper2.findOneNewById(id);
        int i = newsMapper2.addNewClick(id);
        if (oneNewById!=null){
            return new BaseResp(1,"success",oneNewById,null);
        }
        return null;
    }

    /**前端首页展示热度前8的新闻
     * @return 新闻实体类的集合
     */
    @Override
    public BaseResp findMainNews() {
        List<News> mainNews = newsMapper2.findMainNews();
        System.out.println(mainNews);
        if (mainNews!=null){
            return new BaseResp(1,"success",mainNews,null);
        }
        return null;
    }
}
